package com.site.wisdom_construction.controller.healthEpidemicModel;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.responseEntity.TwoDataResult;
import com.site.wisdom_construction.service.healthEpidemicModel.*;
import com.site.wisdom_construction.statisticsResult.WarningRecordCount;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/7
 * @Description
 */
@Api(tags = "防疫手环信息接口")
@RestController
@RequestMapping("AntiepidemicWristband")
@CrossOrigin
public class AntiepidemicWristbandController {
    @Autowired
    private AntiepidemicWristbandService antiepidemicWristbandService;

    @Autowired
    private WarningRecordService warningRecordService;

    @Autowired
    private AnimalHeatHistoryService animalHeatHistoryService;

    @Autowired
    private BloodOxygenHistoryService bloodOxygenHistoryService;

    @Autowired
    private BloodPressureHistoryService bloodPressureHistoryService;

    @Autowired
    private HeartRateHistoryService heartRateHistoryService;

    @ApiOperation(value = "增加防疫手环信息", notes = "测试数据:")
    @PostMapping("/insertAntiepidemicWristband")
    public JsonResult insertAntiepidemicWristband(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        map.put("updateTime", TimeUtils.getNowTimeS());
        int i = antiepidemicWristbandService.insertAntiepidemicWristband(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入防疫手环信息!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入防疫手环信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除防疫手环信息", notes = "测试数据:")
    @PostMapping("/deleteAntiepidemicWristband")
    public JsonResult deleteAntiepidemicWristband(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            mapR.put("id", list.get(i));
            int a = antiepidemicWristbandService.deleteAntiepidemicWristband(mapR);
            l += a;
        }
        if (l >= 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除防疫手环信息!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除防疫手环信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改防疫健康", notes = "测试数据:")
    @PostMapping("/updateAntiepidemicWristband")
    public JsonResult updateAntiepidemicWristband(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        map.put("updateTime", TimeUtils.getNowTimeS());
        int i = antiepidemicWristbandService.updateAntiepidemicWristband(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改防疫手环信息!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改防疫手环信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询防疫健康", notes = "测试数据:")
    @PostMapping("/findAntiepidemicWristbandByPage")
    public Page findAntiepidemicWristbandByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(antiepidemicWristbandService.Total(map));
        page.setItems(antiepidemicWristbandService.findAntiepidemicWristbandByPage(map));
        return page;
    }

    @ApiOperation(value = "查询手环异常人员", notes = "测试数据:")
    @PostMapping("/findAntiepidemicWristbandByWristbandStatus")
    public OneDataResult findAntiepidemicWristbandByWristbandStatus(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(antiepidemicWristbandService.findAntiepidemicWristbandByWristbandStatus(map));
        return oneDataResult;
    }

    @ApiOperation(value = "查询所有手环人员", notes = "测试数据:")
    @PostMapping("/selectAntiepidemicWristband")
    public OneDataResult selectAntiepidemicWristband(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(antiepidemicWristbandService.selectAntiepidemicWristband(map));
        return oneDataResult;
    }

    @ApiOperation(value = "统计手环总数", notes = "测试数据:")
    @PostMapping("/countAntiepidemicWristband")
    public OneDataResult countAntiepidemicWristband(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(antiepidemicWristbandService.countAntiepidemicWristband(map));
        return oneDataResult;
    }

    @ApiOperation(value = "定位手环数据上传", notes = "测试数据:")
    @PostMapping("/uploadWristband")
    @NoToken
    public void uploadWristband(@RequestBody Map map) {
        System.out.println("map:"+map);
        Map mapD = new HashMap();
        Integer pushType = (Integer) map.get("pushType");
        mapD.put("equipmentNumber", map.get("MID"));
        mapD.put("updateTime", map.get("Time"));
        mapD.put("uploadTime", map.get("Time"));
        mapD.put("projectId", 4);

        //体温、心率、血压、血氧推送
        if (pushType==2) {

            if (map.containsKey("H")) {
                //心率
                int H = (int) map.get("H");
                if (H >= 50 && H <= 150) {
                    mapD.put("heartRateStatus", "正常");
                    mapD.put("status","正常");
                } else {
                    mapD.put("heartRateStatus", "异常");
                    mapD.put("wristbandStatus", "异常");
                    mapD.put("warningType", "心率");
                    mapD.put("warningValue", H);
                    mapD.put("status","异常");
                    warningRecordService.insertWarningRecord(mapD);
                }
                mapD.put("heartRate", H);
                antiepidemicWristbandService.updateAntiepidemicWristband(mapD);
                heartRateHistoryService.insertHeartRateHistory(mapD);
            } else if (map.containsKey("O")) {
                //血氧
                String OO = (String) map.get("O");
                int O = Integer.parseInt(OO);
                if (O >= 85 && O <= 100) {
                    mapD.put("bloodOxygenStatus", "正常");
                    mapD.put("status","正常");
                } else {
                    mapD.put("bloodOxygenStatus", "异常");
                    mapD.put("wristbandStatus", "异常");
                    mapD.put("warningType", "血氧");
                    mapD.put("warningValue", OO);
                    mapD.put("status","异常");
                    warningRecordService.insertWarningRecord(mapD);
                }
                mapD.put("bloodOxygen", O);
                antiepidemicWristbandService.updateAntiepidemicWristband(mapD);
                bloodOxygenHistoryService.insertBloodOxygenHistory(mapD);

            } else if (map.containsKey("W")) {
                //体温
                Double W = (Double) map.get("W");
                if (W >= 35.5 && W <= 37.5) {
                    mapD.put("animalHeatStatus", "正常");
                    mapD.put("status","正常");
                } else {
                    mapD.put("animalHeatStatus", "异常");
                    mapD.put("wristbandStatus", "异常");
                    mapD.put("warningType", "体温");
                    mapD.put("warningValue", W);
                    mapD.put("status","异常");
                    warningRecordService.insertWarningRecord(mapD);
                }
                mapD.put("animalHeat", W);
                antiepidemicWristbandService.updateAntiepidemicWristband(mapD);
                animalHeatHistoryService.insertAnimalHeatHistory(mapD);
            } else if (map.containsKey("X")) {
                //血压
                String XX = (String) map.get("X");
                String YY = (String) map.get("Y");
                int X = Integer.parseInt(XX);
                int Y = Integer.parseInt(YY);
                if (X >= 90 && X <= 139 && Y >= 60 && Y <= 89) {
                    mapD.put("bloodPressureStatus", "正常");
                    mapD.put("status","正常");
                } else {
                    mapD.put("bloodPressureStatus", "异常");
                    mapD.put("wristbandStatus", "异常");
                    mapD.put("warningType", "血压");
                    mapD.put("warningValue", XX);
                    mapD.put("status","异常");
                    warningRecordService.insertWarningRecord(mapD);
                }
                mapD.put("bloodPressureHigh", X);
                mapD.put("bloodPressureLow", Y);
                antiepidemicWristbandService.updateAntiepidemicWristband(mapD);
                bloodPressureHistoryService.insertBloodPressureHistory(mapD);
            }
        } else if (pushType==3) {
            //定位推送
            //高德经纬度转百度

            Double x = (Double) map.get("Lon");
            Double y = (Double) map.get("Lat");
            Double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
            Double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sqrt(y * x_pi);
            Double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
            Double lon = z * Math.cos(theta) + 0.0065;
            Double lat = z * Math.sin(theta) + 0.006;
            BigDecimal valueDecimal = new BigDecimal(lon);
            BigDecimal valueDecimalL = new BigDecimal(lat);

            double longitude = valueDecimal.setScale(3, BigDecimal.ROUND_DOWN).doubleValue();
            double latitude = valueDecimalL.setScale(3, BigDecimal.ROUND_DOWN).doubleValue();

            mapD.put("longitude", longitude);
            mapD.put("latitude", latitude);
            mapD.put("province", map.get("Pro"));
            mapD.put("city", map.get("City"));
            mapD.put("district", map.get("Dist"));
            mapD.put("street", map.get("Str"));
            antiepidemicWristbandService.updateAntiepidemicWristband(mapD);
        } else if (pushType==7) {
            //进出围栏推送
            System.out.println("map:" + map);
        }
    }

    @ApiOperation(value = "定位轨迹查询", notes = "测试数据:")
    @PostMapping("/appFootprintQuery")
    @NoToken
    public OneDataResult appFootprintQuery(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObjectR = new JSONObject();
        HttpRequest httpRequest =new HttpRequest();
        String path = "http://fzd.xcloudtech.com:9090/DeviceMonitor/appFootprintQuery.action";
        jsonObject.put("UID",map.get("UID"));
        jsonObject.put("date",map.get("date"));
        jsonObjectR = httpRequest.JsonData(path,jsonObject);
        oneDataResult.setData(jsonObjectR);
        return oneDataResult;
    }

}
